package cn.kgc.fcm.springbootmybatisplusexample.service.impl;

import cn.kgc.fcm.springbootmybatisplusexample.dto.ResultDTO;
import cn.kgc.fcm.springbootmybatisplusexample.entity.Menu;
import cn.kgc.fcm.springbootmybatisplusexample.mapper.MenuMapper;
import cn.kgc.fcm.springbootmybatisplusexample.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    private Logger log = LoggerFactory.getLogger(MenuServiceImpl.class);


    @Override
    public ResultDTO<List<Menu>> selectMenuAll() {

        List<Menu> menus = menuMapper.selectList(null);
        if (CollectionUtils.isEmpty(menus)){
            return ResultDTO.buildError("没有数据");
        }
        return ResultDTO.buildSuccess(menus);
    }

    /**
     * 指定要查询的字段
     * @return
     */
    @Override
    public ResultDTO<List<Menu>> selectMenuAllZDY() {

        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        //指定要查询的字段为name
        List<Menu> menus = menuMapper.selectList(menuQueryWrapper.select("name"));

        return ResultDTO.buildSuccess(menus);
    }

    @Override
    public ResultDTO<List<Menu>> selectMenuAllActiveRecord() {

        Menu menu = new Menu();
        //使用实体类对象自己查询
        List<Menu> menus = menu.selectList(null);

        return ResultDTO.buildSuccess(menus);
    }

    @Override
    public ResultDTO<List<Menu>> selectMenuAllPage(Page<Menu> page) {

        Page<Menu> menuPage = menuMapper.selectPage(page, null);
        log.info("总条数：{}",menuPage.getTotal());
        log.info("总页数：{}",menuPage.getPages());
        log.info("当前页:{}",menuPage.getCurrent());
        log.info("每页{}条",menuPage.getSize());

        return ResultDTO.buildSuccess(menuPage.getRecords());
    }

    @Override
    public ResultDTO<Menu> selectMenuById(Integer id) {

        Menu menu = menuMapper.selectById(id);

        if (Objects.isNull(menu)){
            return ResultDTO.buildError("没有数据");
        }

        return ResultDTO.buildSuccess(menu);
    }

    @Override
    public ResultDTO addMenu(Menu menu) {

        //添加增加数据是时间
//        menu.setCreateTime(new Date());

        int insert = menuMapper.insert(menu);
        if (insert == 0){
            return ResultDTO.buildError("添加失败");
        }

        return ResultDTO.buildSuccess();
    }

    @Override
    public ResultDTO updateMenu(Menu menu) {

        int i = menuMapper.updateById(menu);

        if (i == 0 ){
            return ResultDTO.buildError("修改失败");
        }

        return ResultDTO.buildSuccess();
    }

    @Override
    public ResultDTO deleteMenuById(Integer id) {

        int i = menuMapper.deleteById(id);

        if (i == 0){
            return ResultDTO.buildError("删除失败");
        }

        return ResultDTO.buildSuccess();
    }
}
